package com.xx.ems.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.xx.ems.model.Users;
import com.xx.ems.system.BaseController;

/**
 * @author xxx
 * @date 2014-3-2
 * @version 1.0
 * 描述: 设备领用操作
 */
@Controller
@RequestMapping(value="equipmentReceive", method=RequestMethod.POST)
@SessionAttributes(value="loginuser")
public class EquipmentReceiveController extends BaseController {
	/**
	 * 描述: 领用设备
	 * @param eidArray
	 * @param uid
	 * @param response
	 * @param user
	 * @date 2014-3-2
	 * 
	 * json格式：
	 * var data = {eidArray:"1,2",uid:"sds"};
	 * $.ajax({
	 * 		url:"/ems/equipmentReceive/receiveEquipment",
	 * 		type:"POST",
	 * 		data:data,
	 * 		dataType:"json",
	 * 		success:function(data){
	 * 			alert(data.result);
	 * 		}
	 * });
	 */
	@RequestMapping(value="/receiveEquipment")
	public void receiveEquipment(String eidArray, int uid, HttpServletResponse response, @ModelAttribute(value="loginuser") Users user) {
		try{
			boolean result = getEquipmentReceiveService().receiveEquipment(eidArray, uid, user);
			JSONObject json = new JSONObject();
			response.setContentType("application/json");
			if (result) {
				json.put("result", "suc");
			}else {
				json.put("result", "fail");
			}
			response.getWriter().println(json.toString());
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 描述: 归还设备 
	 * @param eidArray
	 * @param response
	 * @param user
	 * @date 2014-3-4
	 */
	public void restoreEquipment(String eidArray, HttpServletResponse response, @ModelAttribute(value="loginuser") Users user) {
		try{
			boolean result = getEquipmentReceiveService().restoreEquipment(eidArray, user);
			JSONObject json = new JSONObject();
			response.setContentType("application/json");
			if (result) {
				json.put("result", "suc");
			}else {
				json.put("result", "fail");
			}
			response.getWriter().println(json.toString());
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 描述: 获取领用单号列表 
	 * @param pageNow
	 * @param maxCount
	 * @param response
	 * @return
	 * @date 2014-3-4
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/getReceiveNumList")
	public void getReceiveNumList(@RequestParam(defaultValue="1") int pageNow, @RequestParam(defaultValue="8") int maxCount, HttpServletResponse response) {
		try{
			Map<String, Object> map = getEquipmentReceiveService().getReceiveNumList(pageNow, maxCount);
			JSONObject json = new JSONObject();
			response.setContentType("application/json");
			List<Object> list = (List<Object>) map.get("list");
			if (list.size() > 0) {
				json.put("result", list);
			}else {
				json.put("result", "empty");
			}
			json.put("pageNow", pageNow);
			json.put("totalpage", map.get("totalpage"));
			response.setCharacterEncoding("UTF-8");
			response.getWriter().println(json.toString());
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 描述: 根据领用单号id获取相应领用信息 
	 * @param rid
	 * @param response
	 * @date 2014-3-4
	 * 
	 * json格式：
	 * var data = {rid:"12212"};
	 * $.ajax({
	 * 		url:"/ems/equipmentReceive/getReceiveByReceiveNumId",
	 * 		type:"POST",
	 * 		data:data,
	 * 		dataType:"json",
	 * 		success:function(data){
	 * 			alert(data.result);
	 * 		}
	 * });
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/getReceiveByReceiveNumId")
	public void getReceiveByReceiveNumId(int rid, @RequestParam(defaultValue="1") int pageNow, @RequestParam(defaultValue="8") int maxCount, HttpServletResponse response) {
		try{
			Map<String, Object> map = getEquipmentReceiveService().getReceiveByReceiveNumId(rid, pageNow, maxCount);
			JSONObject json = new JSONObject();
			response.setContentType("application/json");
			List<Object> list = (List<Object>) map.get("list");
			if (list.size() > 0) {
				json.put("result", list);
			}else {
				json.put("result", "empty");
			}
			json.put("pageNow", pageNow);
			json.put("totalpage", map.get("totalpage"));
			response.setCharacterEncoding("UTF-8");
			response.getWriter().println(json.toString());
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
}